﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 05/01/2009
// Time: 22:25
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion

using System;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Customer.
	/// </summary>
	public class Customer : csharp.common.DefaultClass, csharp.common.IClass
	{		
		public Person person = new Person();
		
		#region private declarations
	
		
		#endregion
		
		#region properties

		
			
		#endregion
		
		public Customer()
		{
			clear();
			gsql.table= "common.customer";
			_sequence = "common.customer_customer_id_seq";
		}
		
		public void clear()
		{
			person.clear();
		}
		
		
		public override void get(System.Data.DataRow dr)
		{
			_id 		 = dr["customer_id"].ToString();
			person.id = dr["customer_person_id"].ToString();
			person.get();
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id 	   , "customer_id");
			gsql.setValue(person.id , "customer_person_id");
		}
	}
	
	public class Customers : csharp.common.DefaultList, csharp.common.IList
	{
		public Customers()
		{
			gsql.table = "common.customer";
		}
		
		
		/*public List<Customer> items
		{
			get { return items; }
		}*/
		
		
		public Customer get(Int32 index)
		{
			return (Customer)items[index];
		}
		
		public Customer get()
		{
			return (Customer)items[index];
		}		
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.sql= "SELECT * "
						 +"FROM common.customer "
						 +"JOIN common.person ON customer_person_id = person_id "
						 +"WHERE person_company_id = "+Information.companyId;
			
			return mountList();
		}
		
		public bool fillListActive()
		{
			gsql.prepare();
			gsql.sql= "SELECT * "
						 +"FROM common.customer "
						 +"JOIN common.person ON customer_person_id = person_id  AND person_enable "
						 +"WHERE person_company_id = "+Information.companyId;
			
			return mountList();
		}		
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.executeSql())
				return false;
			
			tam = gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				Customer e = new Customer();
				e.get(gsql.getRow(i));
				items.Add(e);
			}
			return true;
		}
		
		//TODO: Verificar uma melhor forma de fazer a busca, BinarySearch, por exemplo
		//Olhar o form frmImportarHorariosRelogioHenry
		public bool searchByCode(string code)
		{
			//items.Sort(
			int tam = items.Count;
			for(int i = 0; i < tam; i++)
			{
				if (((Customer)items[i]).person.code.Equals(code))
				{
					index = i;
					return true;
				}
			}
			return false;
		}
		
		public bool searchByPersonId(string personId)
		{
			int tam = items.Count;
			for(int i = 0; i < tam; i++)
			{
				if (((Customer)items[i]).person.id.Equals(personId))
				{
					index = i;
					return true;
				}
			}
			return false;
		}
	
	}

}
